import { Order } from '@libs/db/models/order.model';
import {
  Body,
  Controller,
  Get,
  Param,
  Patch,
  Query,
  Req,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { OrdersService } from './orders.service';

@ApiTags('订单相关')
@Controller('orders')
export class OrdersController {
  constructor(private readonly orderService: OrdersService) {}

  @ApiOperation({ summary: '获取用户订单' })
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @Get()
  findAll(@Req() request: any, @Query() query: any) {
    return this.orderService.findAll(request.user._id, query);
  }

  @ApiOperation({ summary: '获取当前目标订单' })
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @Get('current')
  findCurrentOrder(@Req() request: any) {
    return this.orderService.findOne(request.user._id);
  }

  @ApiOperation({ summary: '改变订单状态' })
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @Patch(':id')
  updateOrderStatus(@Param('id') id: string, @Body() order: Order) {
    return this.orderService.updateOne(id, order.status);
  }
}
